package com.bit.cfdi.web.utils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

@Repository
public class EncryptionUtils {
	
	protected Logger log = LogManager.getLogger(this.getClass().getName());

	public String stringEncryption(final String stringToEncrypt) {
		
		String toReturn = new String();
		MessageDigest md;
		try {
			md = MessageDigest.getInstance("MD5");
			md.update(stringToEncrypt.getBytes(), 0, stringToEncrypt.length());
			toReturn = new BigInteger(1,md.digest()).toString(16);
			while(toReturn.length()<32) {
				toReturn = "0" + toReturn;
			}
		} catch (NoSuchAlgorithmException e) {
			log.error("Unable to encrypt password: NoSuchAlgorithmException " +
					"Returning un-encrypted string");
			return stringToEncrypt;
		}
		
		return toReturn;
	}
	
}